using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace ShevaContentPipeline.Processors.AnimatedModels
{

    /// <summary>
    /// Assists in creating palette indices for mesh bones.
    /// </summary>
    public sealed class BoneIndexer
    {
        private int currentIndex = 0;
        private Dictionary<string, int> dict = new Dictionary<string, int>();
        private List<string> skinnedBoneNames = new List<string>();

        /// <summary>
        /// The names of the skinned bones that have indices attached to this indexer.
        /// </summary>
        public ReadOnlyCollection<string> SkinnedBoneNames
        {
            get { return skinnedBoneNames.AsReadOnly(); }
        }

        /// <summary>
        /// True if an index has been created for the given bone.
        /// </summary>
        /// <param name="boneName">The name of the bone.</param>
        /// <returns>True if an index has been created for the given bone.</returns>
        public bool ContainsBone(string boneName)
        {
            return dict.ContainsKey(boneName);
        }

        /// <summary>
        /// Creates an index for a bone if one doesn't exist, and returns the palette
        /// index for the given bone.
        /// </summary>
        /// <param name="boneName">The name of the bone.</param>
        /// <returns>The matrix palette index of the bone.</returns>
        public byte GetBoneIndex(string boneName)
        {
            if (!dict.ContainsKey(boneName))
            {
                dict.Add(boneName, currentIndex);
                skinnedBoneNames.Add(boneName);
                currentIndex++;
                return (byte)(currentIndex - 1);
            }
            else
                return (byte)dict[boneName];
        }
        
    }
}
